package com.qf.service;

import com.qf.constant.CacheConstants;
import com.qf.constant.OperatorConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.util.CheckMobileOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * projectName: wokepase1
 *
 * @author: 强力
 * time: 2021/10/18 16:18
 * description:号段补全策略,根据发送的目标手机号识别运行商
 */
@Service(value = "phase")
@Slf4j
public class PhaseServiceImpl implements FilerChainService{

    @Autowired
    private CacheFeign cacheFeign;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("[号段补全策略模块]号段补全开始执行,StandardSubmit={}",standardSubmit);

        //1.获取发送手机号
        String destMobile = standardSubmit.getDestMobile();
        //2.根据目标手机号判断出对应的运营商
            //判断号码是否为中国移动
        if (CheckMobileOperator.isChinaMobilePhoneNum(destMobile) ){
                //运营商id
            standardSubmit.setOperatorId(OperatorConstants.CHINA_MOBILE);
                //下发网关id
            standardSubmit.setGatewayID(OperatorConstants.CHINA_MOBILE);

        }
            //判断号码是否为中国联通
        else if (CheckMobileOperator.isChinaUnicomPhoneNum(destMobile)) {
            //运营商id
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNICOM);
            //下发网关id
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNICOM);
        }
            //判断号码是否为中国电信
        else if (CheckMobileOperator.isChinaTelecomPhoneNum(destMobile)){
            //运营商id
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNICOM);
            //下发网关id
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNICOM);
        }
            //其他渠道号码
        else {
            //运营商id
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNKNOWN);
            //下发网关id
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNKNOWN);
        }

        //3.根据目标手机号和redis中获取到的号段数据,判断出号码的归属地
            //目标手机号前七位截取出来作为判断标准
        String key = destMobile.substring(0, 7);
            //根据key获取号段数据
        String value = cacheFeign.get(CacheConstants.CACHE_PREFIX_PHASE+ key);

        if (StringUtils.isEmpty(value)) {
            log.info("[号段补全策略模块]value查询不到归属地,目标手机号destMobile={}",destMobile);
        return false;
        }
            //切割获取省和市行政区代号
        String[] proviceAndCity = value.split(",");
        if (proviceAndCity.length !=2){
            log.info("[号段补全策略模块]proviceAndCity查询不到归属地,destMobile={}",destMobile);

        }

        standardSubmit.setProvinceId(Integer.parseInt(proviceAndCity[0]));
        standardSubmit.setCityId(Integer.parseInt(proviceAndCity[1]));
        //4.获取redis中路由的数据
            //根据客户端id+网关id到数据库中获取路由通道数据
        Map<String,Object> routerMap = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_ROUTER
                + standardSubmit.getClientID() + ":"
                + standardSubmit.getGatewayID());

        //5获取redis中的通道数据
             //根据运营商id+网关id到数据库中获取路由通道数据
        Map<String,Object> channelMap = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_SMS_CHANNEL
                + standardSubmit.getOperatorId() + ":"
                + standardSubmit.getGatewayID());

        //6.根据路由数据和通道数据以及目标手机号,匹配出短信的下发源号码,以及当前短信的费用
        if (routerMap!=null && channelMap !=null){
            //账号接入号
            String spnumber = String.valueOf(channelMap.get("spnumber"));
            //扩展号
            String extendnumber = String.valueOf(routerMap.get("extendnumber"));
            //下发源号码
            String srcnamber =spnumber+extendnumber;
            standardSubmit.setSrcNumber(srcnamber);

            //当前短信费用标准
            standardSubmit.setFee(Long.parseLong(String.valueOf(routerMap.get("price"))));
        }

        log.info("[号段补全策略模块]号段补全完成,standardSubmit={}",standardSubmit);

        return true;
    }



}
